<?php defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class DocarioControllerGroup extends JController {
    function __construct() {
        parent::__construct();
        
        if (JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'default');
        }
        $this->item_type = 'Default';
        $this->registerTask('addgroup', 'editgroup');
    }
    
    function editgroup() {
        $view =& $this->getView('editgroup', 'html');
        
        $model = $this->getModel('group');
        $view->setModel($model, true);
        
        $view->display();
    }
    
    function deletegroup() {
        $cid = JRequest::getVar('cid', array(0));
    
        $model = $this->getModel('group');
        $type = 'message';
        
        if (!is_array($cid) || count($cid) < 1) {
            $msg = JText::_('SELECT_DELETE');
            $type = 'warning';
        } else {
            foreach ($cid as $id) {
                if (!$model->deleteGroup($id)) {
                    $msg = JText::_('DELETE_FAILED');
                    $type = 'error';
                }
            }
            
            $msg = count($cid) . ' ' . JText::_('SELECTED_DELETE');
            $cache = &JFactory::getCache('com_docario');
            $cache->clean();
        }
        
        $this->setRedirect(JRoute::_('index.php?option=com_docario&task=groups'), 
                           $msg, $type);
    }
    
    function savegroup() {
        $model = $this->getModel('group');
        $type = 'message';
        
        if ($model->save()) {
            $msg = JText::_('GROUP_SAVED');
        } else { 
            $msg = JText::_('GROUP_SAVE_ERROR');
            $type = 'error';
        }
        
        $this->setRedirect(JRoute::_('index.php?option=com_docario&task=groups'),
                           $msg, $type);
    }
    
    function getJUsersJSON() {
        $document = JFactory::getDocument();
        $document->setMimeEncoding('application/json');
        
        $view =& $this->getView('usersjson', 'raw');
        
        $view->setModel($this->getModel('group'), true);
        
        $view->display();
    }
    
    function addUser() {
        $user_id = JRequest::getVar('uid');
        $group_id = JRequest::getVar('gid');
        
        $model = $this->getModel('group');
        $model->addMember($group_id, $user_id);
        
        $this->setRedirect(JRoute::_('index.php?option=com_docario&controller=group&task=editGroup&fromList=0&cid='.$group_id));
    }
    
    function removeUser() {
        $user_id = JRequest::getVar('uid');
        $group_id = JRequest::getVar('gid');
        
        $model = $this->getModel('group');
        $model->removeMember($group_id, $user_id);
        
        $this->setRedirect(JRoute::_('index.php?option=com_docario&controller=group&task=editGroup&fromList=0&cid='.$group_id));
    }
    
    function adminOn() {
        $user_id = JRequest::getVar('uid');
        $group_id = JRequest::getVar('gid');
        
        $model = $this->getModel('group');
        $model->adminOn($group_id, $user_id);
        
        $this->setRedirect(JRoute::_('index.php?option=com_docario&controller=group&task=editGroup&fromList=0&cid='.$group_id));
    }
    
    function adminOff() {
        $user_id = JRequest::getVar('uid');
        $group_id = JRequest::getVar('gid');
        
        $model = $this->getModel('group');
        $model->adminOff($group_id, $user_id);
        
        $this->setRedirect(JRoute::_('index.php?option=com_docario&controller=group&task=editGroup&fromList=0&cid='.$group_id));
    }
}
?>
